package com.dream.common.utils.payutils;


import com.dream.common.constans.Constants;
import com.dream.common.utils.EmojiUtils;
import com.dream.common.utils.JsonUtils;
import com.dream.common.utils.payutils.bean.*;
import com.dream.common.utils.payutils.bean.configuration.FunctionSwitchBean;
import com.dream.common.utils.payutils.bean.configuration.NextSenderRuleBean;
import com.dream.common.utils.payutils.bean.configuration.ReportResultBean;
import com.dream.entity.GroupRules;
import com.dream.entity.NextSendContinueBean;
import com.dream.entity.PickUserInfoBean;
import com.dream.service.DelayRelayQueue;
import com.dream.service.basic.BaseServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PayInfoUtils {
    public static RPResultToApp getNextSenderRPResultToApp(BeforeRedPackageBean redPackageBean, WorkedRedPackageBean workedRedPackageBean, GroupRules groupRules) {
        DelayRelayQueue delayRelayQueue = BaseServiceHelper.getDelayRelayQueue();
        Map<String, NextSendContinueBean> nextSendUserInfoMap = delayRelayQueue.nextSendUserInfoMap;
        NextSenderRuleBean nextSenderRule = JsonUtils.parseObject(groupRules.getNextSenderRule(), NextSenderRuleBean.class);
        RPResultToApp rpResultToApp = new RPResultToApp();
        if (workedRedPackageBean.isErrorRedPackage) {
            rpResultToApp.sendString = workedRedPackageBean.errorRedPackageInfo;
            return rpResultToApp;
        }
        ReportResultBean reportResultBean;
        if (groupRules.getPrizeText() == null || "".equals(groupRules.getPrizeText())) {
            reportResultBean = new ReportResultBean();
        } else {
            reportResultBean = JsonUtils.parseObject(groupRules.getPrizeText(), ReportResultBean.class);
        }
        String pickMoneyEndNumberStr = null;
        PickUserInfoBean noDeadUser = null;
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        for (PickUserInfoBean pickUserInfoBean : pickUserList) {
            if (pickMoneyEndNumberStr == null || "".equals(pickMoneyEndNumberStr)) {
                pickMoneyEndNumberStr = "" + NumberUtils.getFen(pickUserInfoBean.pickMoney);
            } else {
                pickMoneyEndNumberStr = pickMoneyEndNumberStr + "-" + NumberUtils.getFen(pickUserInfoBean.pickMoney);
            }
            if (pickUserInfoBean.isNoDead) {
                noDeadUser = pickUserInfoBean;
            }
        }
        int playType = nextSenderRule.nextSenderType;
        String playTypeStr = "";
        switch (playType) {
            case 1:
                if (nextSenderRule.moneyType == 1) {
                    playTypeStr = "比大小-最大发";
                } else {
                    playTypeStr = "比大小-最小发";
                }
                break;
            case 2:
                if (nextSenderRule.endNumberType == 1) {
                    playTypeStr = "比娓数-最大发";
                } else {
                    playTypeStr = "比娓数-最小发";
                }
                break;
            case 3:
                if (nextSenderRule.noDeadType == 1) {
                    playTypeStr = "莬死上级";
                } else {
                    playTypeStr = "莬死下级";
                }
                break;
            case 4:
                if (nextSenderRule.doubleAType == 1) {
                    playTypeStr = "双A最大發";
                } else {
                    playTypeStr = "双A最小發";
                }
                break;
        }
        if (playType == 3 && noDeadUser == null) {
            if (nextSenderRule.noDeadNotPickType == 1) {
                playTypeStr = "莬死未枪比大小";
            } else if (nextSenderRule.noDeadNotPickType == 2) {
                playTypeStr = "莬死未枪比娓数";
            }
        }
        String sendString = "当前頑珐：" + playTypeStr;
        sendString = sendString + "\n發包人员：" + workedRedPackageBean.sendUsername;
        sendString = sendString + "\n發包详情：" + workedRedPackageBean.sendMoney + "-" + workedRedPackageBean.sendNumber;
        if (noDeadUser != null) {
            sendString = sendString + "\n莬死琻额：" + EmojiUtils.getEmojiStringByUnicode(0x1F4b0) + noDeadUser.pickMoney;
        }
        if (playType == 2 || (playType == 3 && noDeadUser == null && nextSenderRule.noDeadNotPickType == 2)) {
            sendString = sendString + "\n娓数详情：" + EmojiUtils.getEmojiStringByUnicode(0x1F4B9) + pickMoneyEndNumberStr;
        }
        List<PickUserInfoBean> nextSendUserList = new ArrayList();
        for (PickUserInfoBean pickUserInfoBean : pickUserList) {
            if (pickUserInfoBean.rewardsMoney != null && pickUserInfoBean.rewardsMoney != 0) {
                sendString = sendString + "\n奬厉人员：" + EmojiUtils.getEmojiStringByUnicode(0x1F389) + pickUserInfoBean.username;
                sendString = sendString + "\n奬厉详情：" + EmojiUtils.getEmojiStringByUnicode(0x1F389) + pickUserInfoBean.pickMoney + "-" + pickUserInfoBean.rewardsMoney;
            }
            if (pickUserInfoBean.isNextSendUser) {
                nextSendUserList.add(pickUserInfoBean);
            }
        }
        if (nextSendUserList.size() > 1) {//双A頑珐，不要连续伐包次数统计
            for (PickUserInfoBean pickUserInfoBean : nextSendUserList) {
                sendString = sendString + "\n接包人员：" + EmojiUtils.getEmojiStringByUnicode(0x1F624) + "【" + pickUserInfoBean.username + "】";
                sendString = sendString + "\n接包琻额：" + pickUserInfoBean.pickMoney;
            }
        } else {
            PickUserInfoBean pickUserInfoBean = nextSendUserList.get(0);
            sendString = sendString + "\n接包人员：" + EmojiUtils.getEmojiStringByUnicode(0x1F624) + "【" + nextSendUserList.get(0).username + "】";
            NextSendContinueBean nextSendContinueBean = nextSendUserInfoMap.get(workedRedPackageBean.bijiAccount);
            if (nextSendContinueBean != null) {
                if (pickUserInfoBean.userID.equals(nextSendContinueBean.userId)) {
                    nextSendContinueBean.continueTimes = nextSendContinueBean.continueTimes + 1;
                } else {
                    nextSendContinueBean.userId = pickUserInfoBean.userID;
                    nextSendContinueBean.continueTimes = 1;
                }
            } else {
                nextSendContinueBean = new NextSendContinueBean();
                nextSendContinueBean.continueTimes = 1;
                nextSendContinueBean.userId = pickUserInfoBean.userID;
            }
            sendString = sendString + "\n接包详情：" + pickUserInfoBean.pickMoney + "--" + nextSendContinueBean.continueTimes + "连";
            nextSendUserInfoMap.put(workedRedPackageBean.bijiAccount, nextSendContinueBean);
        }
        //拼接收尾
        if (reportResultBean.customBegin != null && !"".equals(reportResultBean.customBegin)) {
            sendString = reportResultBean.customBegin + "\n" + sendString;
        }
        if (reportResultBean.customEnd != null && !"".equals(reportResultBean.customEnd)) {
            sendString = sendString + "\n" + reportResultBean.customEnd;
        }
        rpResultToApp.sendString = sendString;
        return rpResultToApp;
    }

    public static RPResultToApp getForbidRPResultToApp(BeforeRedPackageBean redPackageBean, WorkedRedPackageBean workedRedPackageBean, GroupRules groupRules) {
        RPResultToApp rpResultToApp = new RPResultToApp();
        if (workedRedPackageBean.isErrorRedPackage) {
            rpResultToApp.sendString = workedRedPackageBean.errorRedPackageInfo;
            return rpResultToApp;
        }
        ReportResultBean reportResultBean;
        if (groupRules.getPrizeText() == null || "".equals(groupRules.getPrizeText())) {
            reportResultBean = new ReportResultBean();
        } else {
            reportResultBean = JsonUtils.parseObject(groupRules.getPrizeText(), ReportResultBean.class);
        }
        FunctionSwitchBean functionSwitchBean;
        functionSwitchBean = JsonUtils.parseObject(groupRules.getFeatureSwitch(), FunctionSwitchBean.class);
        String sendString;
        String pickMoneyStr = "";
        String pickMoneyEndStr = "";
        String stealMoneyEndStr = "";
        List<String> kickUserIdList = new ArrayList<>();
        List<String> addBlackUserIdList = new ArrayList<>();
        for (PickUserInfoBean pickUserInfoBean : workedRedPackageBean.pickUserList) {
            if (pickMoneyStr == null || "".equals(pickMoneyStr)) {
                pickMoneyStr = pickMoneyStr + pickUserInfoBean.pickMoney;
            } else {
                pickMoneyStr = pickMoneyStr + "-" + pickUserInfoBean.pickMoney;
            }
            if (pickMoneyEndStr == null || "".equals(pickMoneyEndStr)) {
                pickMoneyEndStr = pickMoneyEndStr + NumberUtils.getFen(pickUserInfoBean.pickMoney);
            } else {
                pickMoneyEndStr = pickMoneyEndStr + "-" + NumberUtils.getFen(pickUserInfoBean.pickMoney);
            }
            if (pickUserInfoBean.isNoPicker) {
                if (stealMoneyEndStr == null || "".equals(stealMoneyEndStr)) {
                    stealMoneyEndStr = stealMoneyEndStr + "\n" + pickUserInfoBean.pickMoney + "-" + pickUserInfoBean.username;
                } else {
                    stealMoneyEndStr = stealMoneyEndStr + "\n" + pickUserInfoBean.pickMoney + "-" + pickUserInfoBean.username;
                }
            }
        }
        if (reportResultBean.custom == null || "".equals(reportResultBean.custom.trim())) {
            sendString = Constants.REPORT_TEXT;
        } else {
            sendString = reportResultBean.custom;
        }
        sendString = sendString.replace("#FBNC", workedRedPackageBean.sendUsername)
                .replace("#FBJE", workedRedPackageBean.sendMoney + "")
                .replace("#FBGS", workedRedPackageBean.sendNumber + "")
                .replace("#FBBM", workedRedPackageBean.wishing)
                .replace("#QBJE", pickMoneyStr)
                .replace("#JEWS", pickMoneyEndStr)
                .replace("#FJJE", workedRedPackageBean.thunderMoney + "");
        if (stealMoneyEndStr == null || "".equals(stealMoneyEndStr)) {
            sendString = sendString.replace("#TBXX", "");
        } else {
            sendString = sendString.replace("#TBXX", "\n偷包信息：" + stealMoneyEndStr);
        }
        //拼接收尾
        if (reportResultBean.customBegin != null && !"".equals(reportResultBean.customBegin)) {
            rpResultToApp.sendString = reportResultBean.customBegin + "\n：" + sendString;
        }
        if (reportResultBean.customEnd != null && !"".equals(reportResultBean.customEnd)) {
            rpResultToApp.sendString = sendString + "\n：" + reportResultBean.customEnd;
        }
        if (reportResultBean.payMessageType == 0) {
            rpResultToApp.sendString = sendString;
        } else if (reportResultBean.payMessageType == 1) {
            if (workedRedPackageBean.thunderMoney > 0) {
                rpResultToApp.sendString = sendString;
            }
        }
        if (functionSwitchBean.isAutoPay) {
            rpResultToApp.payMoney = workedRedPackageBean.thunderMoney + "";
        } else {
            rpResultToApp.payMoney = "0";
        }
        rpResultToApp.password = functionSwitchBean.payPassword;
        if (functionSwitchBean.steal) {
            for (PickUserInfoBean pickUserInfoBean : workedRedPackageBean.pickUserList) {
                if (pickUserInfoBean.isNoPicker) {
                    kickUserIdList.add(pickUserInfoBean.userID);
                    if (functionSwitchBean.kickOut) {
                        addBlackUserIdList.add(pickUserInfoBean.userID);
                    }
                }
            }
        }
        rpResultToApp.kickUserIdList = kickUserIdList;
        rpResultToApp.addBlackUserIdList = addBlackUserIdList;
        if (workedRedPackageBean.isErrorRedPackage) {
            rpResultToApp.sendString = rpResultToApp.sendString + workedRedPackageBean.errorRedPackageInfo;
        }
        rpResultToApp.blessing = "自动赔付：" + workedRedPackageBean.thunderMoney;
        rpResultToApp.userNameId = workedRedPackageBean.sendUserID;
        return rpResultToApp;
    }

    public static MessageBeanOld getMessageBean(int playType, BeforeRedPackageBean redPackageBean, WorkedRedPackageBean workedRedPackageBean, GroupRules groupRules) {
        RedPackageWebBean redPackageWebBean = new RedPackageWebBean();
        //襟枪頑珐前端红包界面显示自定义拼接，其他頑珐跟app发出来的消息一致（去掉自定义首尾）
        if (playType == 0) {
            String forbidShowString = "详情：" + redPackageBean.resultData.totalfee + "-" + redPackageBean.resultData.totalnum;
            forbidShowString = forbidShowString + "\n包名：" + redPackageBean.blessings;
            for (int i = 0; i < redPackageBean.resultData.cxuserredpackagelist.size(); i++) {
                if (i == 0) {
                    forbidShowString = forbidShowString + "\n尾数：" + (int) NumberUtils.multiply(redPackageBean.resultData.cxuserredpackagelist.get(i).money, 100) % 10;
                } else {
                    forbidShowString = forbidShowString + "-" + (int) NumberUtils.multiply(redPackageBean.resultData.cxuserredpackagelist.get(i).money, 100) % 10;
                }
            }
            forbidShowString = forbidShowString + "\n反奖：" + workedRedPackageBean.thunderMoney;
            redPackageWebBean.isWin = workedRedPackageBean.isWin;
            redPackageWebBean.title = workedRedPackageBean.isWin ? "中" : "未中";
            redPackageWebBean.webShowString = forbidShowString;
        } else {
            NextSenderRuleBean nextSenderRule = JsonUtils.parseObject(groupRules.getNextSenderRule(), NextSenderRuleBean.class);
            String pickMoneyEndNumberStr = null;
            PickUserInfoBean noDeadUser = null;
            List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
            for (PickUserInfoBean pickUserInfoBean : pickUserList) {
                if (pickMoneyEndNumberStr == null || "".equals(pickMoneyEndNumberStr)) {
                    pickMoneyEndNumberStr = "" + NumberUtils.getFen(pickUserInfoBean.pickMoney);
                } else {
                    pickMoneyEndNumberStr = pickMoneyEndNumberStr + "-" + NumberUtils.getFen(pickUserInfoBean.pickMoney);
                }
                if (pickUserInfoBean.isNoDead) {
                    noDeadUser = pickUserInfoBean;
                }
            }
            redPackageWebBean.isWin = true;
            int nextSendPlayType = nextSenderRule.nextSenderType;
            String playTypeStr = "";
            switch (nextSendPlayType) {
                case 1:
                    if (nextSenderRule.moneyType == 1) {
                        playTypeStr = "比大小-最大发";
                    } else {
                        playTypeStr = "比大小-最小发";
                    }
                    break;
                case 2:
                    if (nextSenderRule.endNumberType == 1) {
                        playTypeStr = "比娓数-最大发";
                    } else {
                        playTypeStr = "比娓数-最小发";
                    }
                    break;
                case 3:
                    if (nextSenderRule.noDeadType == 1) {
                        playTypeStr = "莬死上级";
                    } else {
                        playTypeStr = "莬死下级";
                    }
                    break;
                case 4:
                    if (nextSenderRule.doubleAType == 1) {
                        playTypeStr = "双A最大發";
                    } else {
                        playTypeStr = "双A最小發";
                    }
                    break;
            }
            if (playType == 3 && noDeadUser == null) {
                if (nextSenderRule.noDeadNotPickType == 1) {
                    playTypeStr = "大小";
                } else if (nextSenderRule.noDeadNotPickType == 2) {
                    playTypeStr = "尾数";
                }
            }
            redPackageWebBean.title = playTypeStr;
            String nextSendShowString = "详情：" + redPackageBean.resultData.totalfee + "-" + redPackageBean.resultData.totalnum;
            if (noDeadUser != null) {
                nextSendShowString = nextSendShowString + "\n免死：" + noDeadUser.pickMoney;
            }
            if (playType == 2 || (playType == 3 && noDeadUser == null && nextSenderRule.noDeadNotPickType == 2)) {
                nextSendShowString = nextSendShowString + "\n尾数：" + pickMoneyEndNumberStr;
            }
            for (PickUserInfoBean pickUserInfoBean : pickUserList) {
                if (pickUserInfoBean.isNextSendUser) {
                    nextSendShowString = nextSendShowString + "\n接包：" + pickUserInfoBean.username;
                }
            }
            for (PickUserInfoBean pickUserInfoBean : pickUserList) {
                if (pickUserInfoBean.rewardsMoney != null && pickUserInfoBean.rewardsMoney != 0) {
                    nextSendShowString = nextSendShowString + "\n奖励人员：" + pickUserInfoBean.username;
                    nextSendShowString = nextSendShowString + "\n奖励详情：枪" + pickUserInfoBean.pickMoney + "奖" + pickUserInfoBean.rewardsMoney;
                }
            }
            redPackageWebBean.webShowString = nextSendShowString;
        }
        MessageBeanOld messageBean = new MessageBeanOld();
        messageBean.appName = "cx";
        messageBean.userName = redPackageBean.userName;
        messageBean.messageType = 1;
        messageBean.groupId = redPackageBean.groupId;
        messageBean.groupName = redPackageBean.groupName;
        messageBean.sendMessageTime = System.currentTimeMillis();
        messageBean.sendMessageUserId = redPackageBean.sendId;
        messageBean.sendMessageUserName = redPackageBean.sendName;
        messageBean.sendMessageUserImg = redPackageBean.sendHeadImg;
        messageBean.sendMsg = JsonUtils.toJSONString(redPackageWebBean);
        return messageBean;
    }
}
